/*
 * Copyright (c) dabing.io
 * All rights reserved.
 * CrowResultController.java
 */

package com.ueboot.weishop.api.controller.activity;

import com.google.common.collect.Lists;
import com.ueboot.core.exception.BusinessException;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.api.SessionUtils;
import com.ueboot.weishop.api.controller.activity.vo.CrowResultResp;
import com.ueboot.weishop.entity.activity.CrowResultEntity;
import com.ueboot.weishop.service.activity.CrowResultService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 描述: 众筹结果数据查询层代码
 *
 * @author ziv
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/public/activity")
public class CrowResultController {

    @Autowired
    private CrowResultService crowResultService;

    @RequestMapping(value = "/crow/result", method = RequestMethod.POST)
    public Response<List<CrowResultResp>> result() {

        String openId = SessionUtils.getOpenId();
        if (StringUtils.isEmpty(openId)) {
            throw new BusinessException("请使用微信！");
        }
        List<CrowResultEntity> crowResultEntityList = crowResultService.findTop20ByLotteryOrderByOpenDateDesc(CrowResultEntity.LOTTERY_YES);
        List<CrowResultResp> crowResultRespList = Lists.newLinkedList();
        crowResultEntityList.forEach(crowResultEntity -> {
            CrowResultResp resp = new CrowResultResp();
            resp.setGoodsName(crowResultEntity.getGoodsName());
            resp.setPeriodNum(crowResultEntity.getPeriodNum());
            resp.setLotteryNum(crowResultEntity.getLotteryNum());
            crowResultRespList.add(resp);
        });
        return new Response<>(crowResultRespList);
    }
}